package com.huika.renrench.control.investment.activity;

import java.util.List;

import android.support.v4.view.AutoScrollViewPager;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.utils.MoneyShowTool;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.BorrowDetailBean;
import com.huika.renrench.support.bean.ImageBean;
import com.huika.renrench.utils.DateTimeTool;
import com.huika.renrench.view.TitleBarHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 
 * @description：借款人详情
 * @author zc
 * @date 2015年6月2日 上午11:49:02
 */
public class BorrowDetailActivity extends BaseAct implements Listener<RequestResult<BorrowDetailBean>> {

	private TextView name;
	private TextView money_detail;
	private TextView earnings_detail;
	private TextView time_detail;
	private TextView deadline_detail;
	private TextView to_use;
	private ImageView borrower_img;
	private TextView borrower_name;
	private TextView borrower_phone;
	private TextView borrower_shop_name;
	private TextView borrower_shop_address;
	private AutoScrollViewPager<ImageBean> shop_imgs;
	private ImageView cover_iv;
	private BorrowDetailBean bean;
	private DisplayImageOptions options;
	private String userId;
	private String loanId;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_invest_go_detail);
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.icon_default_small)
		// resource or drawable
		.showImageForEmptyUri(R.drawable.icon_default_small)
		.showImageOnFail(R.drawable.icon_default_small)
		.cacheInMemory(true)// 开启内存缓存
		.cacheOnDisk(true) // 开启硬盘缓存
		.resetViewBeforeLoading(true).build();
	}
	
	@Override
	protected void initData() {
		super.initData();
		TitleBarHelper barHelper = new TitleBarHelper(this, -1);
		barHelper.setTitleMsg("借款详情");
		barHelper.setLeftMsg("返回");
		loanId = getIntent().getStringExtra("loanId");
		userId = GlobalApp.getInstance().getUserId();
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		name = (TextView) findViewById(R.id.name);
		money_detail = (TextView) findViewById(R.id.money_detail);
		earnings_detail = (TextView) findViewById(R.id.earnings_detail);
		time_detail = (TextView) findViewById(R.id.time_detail);
		deadline_detail = (TextView) findViewById(R.id.deadline_detail);
		to_use = (TextView) findViewById(R.id.to_use);
		borrower_img = (ImageView) findViewById(R.id.borrower_img);
		borrower_name = (TextView) findViewById(R.id.borrower_name);
		borrower_phone = (TextView) findViewById(R.id.borrower_phone);
		borrower_shop_name = (TextView) findViewById(R.id.borrower_shop_name);
		borrower_shop_address = (TextView) findViewById(R.id.borrower_shop_address);
		shop_imgs = (AutoScrollViewPager<ImageBean>) findViewById(R.id.shop_imgs);
		shop_imgs.setScaleType(ScaleType.FIT_XY);
		shop_imgs.setImageOptions(new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.icon_default_small)
				// resource or drawable
				.showImageForEmptyUri(R.drawable.icon_default_small)
				.showImageOnFail(R.drawable.icon_default_small)
				.cacheInMemory(true)// 开启内存缓存
				.cacheOnDisk(true) // 开启硬盘缓存
				.resetViewBeforeLoading(false).build());
		cover_iv = (ImageView) findViewById(R.id.cover_iv);
//		initDatas();
		getLoanProjectDetail(userId, loanId);
	}
	
	/**
	 * 
	 * @author zc
	 * @date 2015年3月26日,上午10:05:31
	 */
	private void initDatas(List<ImageBean> lists) {
//		List<ImageBean> lists = new ArrayList<ImageBean>();
//		for(int i = 0 ; i < 5; i++){
//			ImageBean bean = new ImageBean("http://image.178pb.com/pm1/M00/00/9D/CqvBj1SiV5yAclt0AABbUZJYjJ8428=295x295.jpg", "");
//			lists.add(bean);
//		}
		cover_iv.setVisibility(View.GONE);
		shop_imgs.refreshDatas(lists,
				(LinearLayout)findViewById(R.id.advertisement_dotLl), false, true);
		shop_imgs.start(3000);
	}
	
	private void getLoanProjectDetail(String userId, String loanId) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		ajaxParams.putCommonTypeParam("loanId", loanId);
		FormResultRequest<BorrowDetailBean> request = new FormResultRequest<BorrowDetailBean>(UrlConstants.GETLOANPROJECTDETAIL, this, this, new TypeToken<RequestResult<BorrowDetailBean>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}

	@Override
	public void onResponse(RequestResult<BorrowDetailBean> arg0) {
		dismissLoadingDialog();
		if(arg0.flag == 1) {
			bean = arg0.getRs();
			name.setText(bean.getTitle());
			money_detail.setText(MoneyShowTool.twolastDF(Double.parseDouble(bean.getTargetAmount())) + "元");
			earnings_detail.setText(bean.getYearRate() + "%");
			time_detail.setText(DateTimeTool.getTimeWithFormat(Long.parseLong(bean.getCreateTime()), "yyyy-MM-dd"));
			deadline_detail.setText(bean.getLoanTime() + "个月");
			to_use.setText(Html.fromHtml("<font color=#969696>借款用途：</font>" + bean.getIntroduction()));
//			borrower_img.setImageResource(-1);
			ImageLoader.getInstance().displayImage(bean.getLoanUserImg(),
					borrower_img, options);
			borrower_name.setText("借款人姓名：" + getHideRealName(bean.getLoanUserName()));
			borrower_phone.setText("借款人电话：" + getHideIdCard(bean.getLoanUserPhone()));
			borrower_shop_name.setText("借款人铺名：" + bean.getShopName());
			if(bean.getShopAddress().length() > 20) {
				borrower_shop_address.setText("店铺地址：\n" + bean.getShopAddress());
			} else {
				borrower_shop_address.setText("店铺地址：" + bean.getShopAddress());
			}
			if(bean.getImageArray() != null && bean.getImageArray().size() > 0) {
				
				initDatas(bean.getImageArray());
			} else {
				cover_iv.setVisibility(View.VISIBLE);
			}
		}
	}
	
	private String getHideRealName(String realName) {
		StringBuffer sb = new StringBuffer(realName.substring(0, 1));
		int count = realName.length() - 1;
		for(int i = 0; i < count; i++) {
			sb.append('*');
		}
		return sb.toString();
	}
	
	private String getHideIdCard(String idCard) {
		StringBuffer sb = new StringBuffer(idCard.substring(0, 3));
		int count = idCard.length() - 7;
		for(int i = 0; i < count; i++) {
			sb.append('*');
		}
		sb.append(idCard.substring(idCard.length() - 4));
		return sb.toString();
	}

}
